﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using System.Runtime.Serialization;
namespace CMN.Entity
{
    /// <summary>
    /// 所有栏目内显示的文章列表信息
    /// </summary>
    public class Module_ListInfo : ISerializable, IEquatable<Module_ListInfo>     
    {

         public override bool Equals(object obj)
         {
             if (obj == null) return false;
            if (object.ReferenceEquals(this, obj)) return true;
             if (this.GetType() != obj.GetType()) return false;
             return Equals(obj as Module_ListInfo);
        }

         public bool Equals(Module_ListInfo other)
         {
             if (other == null) return false;
             if(this == other) return true;
             return Id.Equals(other.Id) ;
        }
        public Module_ListInfo()
        {

        }
        public Module_ListInfo(SerializationInfo info, StreamingContext content)
        {
            Id = info.GetInt32("Id");
            ChannelId = info.GetInt32("ChannelId");
            Path = info.GetString("Path");
            Title = info.GetString("Title");
            TColor = info.GetString("TColor");
            AddDate = info.GetDateTime("AddDate");
            Summary = info.GetString("Summary");
            Author = info.GetString("Author");
            ViewNum = info.GetInt32("ViewNum");
            Img = info.GetString("Img");
            OrderNum = info.GetInt32("OrderNum");
            SupportNum = info.GetInt32("SupportNum");
            Url = info.GetString("Url");
            Flag = info.GetString("Flag");
            Tags = info.GetString("Tags");
            SourceFrom = info.GetString("SourceFrom");
            if(info.MemberCount>16)
            Enabled = info.GetBoolean("Enabled");
           

        }


        public virtual void GetObjectData(SerializationInfo info, StreamingContext content)
        {
            info.AddValue("Id", Id);
            info.AddValue("ChannelId", ChannelId);
            info.AddValue("Path", Path);
            info.AddValue("Title", Title);
            info.AddValue("TColor", TColor);
            info.AddValue("AddDate", AddDate);
            info.AddValue("Summary", Summary);
            info.AddValue("Author", Author);
            info.AddValue("ViewNum", ViewNum);
            info.AddValue("Img", Img);
            info.AddValue("OrderNum", OrderNum);
            info.AddValue("SupportNum", SupportNum);
            info.AddValue("Url", Url);
            info.AddValue("Flag", Flag);
            info.AddValue("Tags", Tags);
            info.AddValue("SourceFrom", SourceFrom);
            info.AddValue("Enabled", Enabled);
            
            
        }
        #region "Private Variables"

        private Int32 _Id = 0; // 文章ID
        private Int32 _ChannelId = 0; // 频道ID
        private string _Path = ""; // 频道规则路径
        private String _Title = string.Empty; // 文章标题
        private String _TColor = string.Empty; // 标题颜色
        private DateTime _AddDate = DateTime.Now; // 添加时间
        private String _Summary = string.Empty; // 摘要
        private String _Editor = string.Empty; // 编辑
        private String _Author = string.Empty; // 作者
        private String _Tags = string.Empty; // 标签
        private Int32 _ViewNum = 0; // 浏览次数
        private String _Img = ""; // 图片地址
        private Int32 _OrderNum = 0; // 排序数
        private Int32 _UserId = 0; // 添加用户ID
        private String _SourceFrom = string.Empty; // 来源
        private Int32 _SupportNum = 0; // 支持数
        private Int32 _AgainstNum = 0; // 反对数量
        private string _Url = string.Empty; //网页地址
        private string _Flag = string.Empty;
        private DateTime? _TopTime ;
        private string _IsAutoImg = "";
        private bool _Enabled = false;
        
        #endregion

        #region "Public Variables"
      
        /// <summary>
        /// 文章ID
        /// </summary>
        public Int32 Id
        {
            set { this._Id = value; }
            get { return this._Id; }
        }
        /// <summary>
        /// 频道ID
        /// </summary>
        public Int32 ChannelId
        {
            set { this._ChannelId = value; }
            get { return this._ChannelId; }
        }
        /// <summary>
        /// 频道规则路径
        /// </summary>
        public string Path
        {
            set { this._Path = value; }
            get { return this._Path; }
        }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled
        {
            get { return _Enabled; }
            set { _Enabled = value; }
        }

      
        /// <summary>
        /// 文章标题
        /// </summary>
        public String Title
        {
            set { this._Title = value; }
            get { return this._Title; }
        }


        /// <summary>
        /// 获取自定长度的标题
        /// </summary>
        /// <param name="returnLength"></param>
        /// <returns></returns>
        public string GetLengthTitle(int returnLength)
        {
            return GetLengthTitle(returnLength, false);
        }

        /// <summary>
        /// 获取自定长度的标题
        /// </summary>
        /// <param name="str"></param>
        /// <param name="returnLength"></param>
        /// <param name="isShowEllipsis"></param>
        /// <returns></returns>
        public   string GetLengthTitle( int returnLength, bool isShowEllipsis)
        {
            string str=this.Title;
            if (!string.IsNullOrEmpty(str))
            {
                if (Encoding.Default.GetBytes(str).Length > returnLength)
                {
                    int str_length = str.Length;

                    int len = 0;

                    for (int i = 0; i < str_length; i++)
                    {
                        byte[] byte_len = Encoding.Default.GetBytes(str.Substring(i, 1));
                        if (byte_len.Length > 1)
                            len += 2;  //如果长度大于1，是中文，占两个字节，+2
                        else
                            len += 1;  //如果长度等于1，是英文，占一个字节，+1

                        if (len >= returnLength)
                        {
                            if (isShowEllipsis)
                                return string.Concat(str.Substring(0, i - 1), "…");
                            else
                                return str.Substring(0, i - 1);
                        }
                    }
                    return null;
                }
                else
                {
                    return str;
                }

            }
            else
            {
                return null;
            }

        }
       
        /// <summary>
        /// 获取新增日期 MM_DD
        /// </summary>
        /// <returns></returns>
        public string GetMDAddDate()
        {
            return this._AddDate.ToString("MM-dd");
        }
        /// <summary>
        /// 获取新增日期 YY_MM_DD
        /// </summary>
        /// <returns></returns>
        public string GetYMDAddDate()
        {
            return this._AddDate.ToString("yy-MM-dd");
        }


        /// <summary>
        /// 获取格式化后新增日期
        /// </summary>
        ///<param name="strFormat">格式化字符串</param>
        /// <returns></returns>
        public string GetFormatAddDate(string strFormat)
        {
            return this._AddDate.ToString(strFormat);
        }



        /// <summary>
        /// 标题颜色
        /// </summary>
        public String TColor
        {
            set { this._TColor = value; }
            get { return this._TColor; }
        }
        /// <summary>
        /// 添加时间
        /// </summary>
        public DateTime AddDate
        {
            set { this._AddDate = value; }
            get { return this._AddDate; }
        }
        /// <summary>
        /// 摘要
        /// </summary>
        public String Summary
        {
            set { this._Summary = value; }
            get { return this._Summary; }
        }
        /// <summary>
        /// 编辑
        /// </summary>
        public String Editor
        {
            set { this._Editor = value; }
            get { return this._Editor; }
        }
        /// <summary>
        /// 作者
        /// </summary>
        public String Author
        {
            set { this._Author = value; }
            get { return this._Author; }
        }
        /// <summary>
        /// 标签
        /// </summary>
        public String Tags
        {
            set { this._Tags = value; }
            get { return this._Tags; }
        }
        /// <summary>
        /// 浏览次数
        /// </summary>
        public Int32 ViewNum
        {
            set { this._ViewNum = value; }
            get { return this._ViewNum; }
        }

        /// <summary>
        /// 图片地址
        /// </summary>
        public String Img
        {
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    this._Img = value;
                }
            }
            get { return this._Img; }
        }

        /// <summary>
        /// 排序数
        /// </summary>
        public Int32 OrderNum
        {
            set { this._OrderNum = value; }
            get { return this._OrderNum; }
        }
        /// <summary>
        /// 添加用户ID
        /// </summary>
        public Int32 UserId
        {
            set { this._UserId = value; }
            get { return this._UserId; }
        }
        /// <summary>
        /// 来源
        /// </summary>
        public String SourceFrom
        {
            set { this._SourceFrom = value; }
            get { return this._SourceFrom; }
        }
        /// <summary>
        /// 支持数
        /// </summary>
        public Int32 SupportNum
        {
            set { this._SupportNum = value; }
            get { return this._SupportNum; }
        }
        /// <summary>
        /// 反对数量
        /// </summary>
        public Int32 AgainstNum
        {
            set { this._AgainstNum = value; }
            get { return this._AgainstNum; }
        }

        /// <summary>
        /// 网页地址
        /// </summary>
        public string Url
        {
            get { return this._Url; }
            set { this._Url = value; }
        }
        /// <summary>
        /// 标记
        /// </summary>
        public string Flag
        {
            get { return this._Flag; }
            set { this._Flag = value; }
        }

        /// <summary>
        /// 居顶截止时间
        /// </summary>
        public DateTime? TopTime
        {
            get { return this._TopTime; }
            set { this._TopTime = value; }
        }

        /// <summary>
        /// 是否自动提取缩略图
        /// </summary>
        public string IsAutoImg
        {
            get { return this._IsAutoImg; }
            set { this._IsAutoImg = value; }
        }
        #endregion
    }
}
